package cn.e3mall.sso.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.e3mall.common.jedis.JedisClient;
import cn.e3mall.common.utils.E3Result;
import cn.e3mall.common.utils.JsonUtils;
import cn.e3mall.pojo.TbUser;
import cn.e3mall.sso.service.TokenService;

@Service
public class TokenServiceImpl implements TokenService {

	@Autowired
	private JedisClient jedisClient;
	
	@Override
	public E3Result getUserInfoByToken(String token) {
		//根据token查询redis的用户信息
		String userInfo = jedisClient.get("SessionId:"+token);
		
		//若查询结果，用户不存在或未登录
		if(StringUtils.isBlank(userInfo)) {
			return E3Result.build(201,"用户未登录或已过期");
		}
		
		//查到数据，用户在登录，重置过期时间
		jedisClient.expire("SessionId:"+token,1800);
		
		TbUser user = JsonUtils.jsonToPojo(userInfo, TbUser.class);
		
		//返回数据
		return E3Result.ok(user);
	}

}
